﻿/* ******************************************************* */
/* Microsoft CRM 4.0 JavaScript Anpassung                  */
/* NotesCount (v0.5m) - CRM-Services 2007                  */
/* netzhaus AG                                             */
/* ******************************************************* */
/* Autor: Thomas Roschinsky (tr@netzhaus.ag)               */
/* Datum: 18.04.2009                                       */
/*                                                         */
/* Projekt: http://notescount.codeplex.com                 */
/* v0.5m: Spezielle Anpassung an Notizen als E-Mail-Anlage */
/* v0.5m: Modified version to work with e-mail attachments */
/*                                                         */
/* Konfiguration:                                          */
var oNotesTab = document.getElementById('tab1Tab');
var oNotesDetailColorFile = "#3C643A";
var oNotesOverview = true;
/*                                                         */
/* ******************************************************* */

function displayNotesOverview(e) {
    try {
        var NotesCountOV = document.getElementById('NotesCountOV');
        var posX = 0;
        var posY = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY) {
            posX = e.pageX;
            posY = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posX = e.clientX + document.body.scrollLeft;
            posY = e.clientY + document.body.scrollTop;
        }
        NotesCountOV.firstChild.style.width = NotesCountOV.style.width;
        NotesCountOV.firstChild.style.left = (posX).toString() + "px";
        NotesCountOV.firstChild.style.top = (posY + 20).toString() + "px";
        NotesCountOV.style.visibility = (e.type == "mouseleave") ? "hidden" : "visible";
    }
    catch (ex) { }
}
function WSRetrieveMultiple(crmEntity, resultAttribute, queryAttribute, queryValue) {
    try {
        var oXMLSoapMsg = "<?xml version='1.0' encoding='utf-8'?>" +
            "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
            " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
            " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
            GenerateAuthenticationHeader().toString() +
            "<soap:Body>" +
            "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
            "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'" +
            " xsi:type='q1:QueryExpression'>" +
            "<q1:EntityName>" + crmEntity + "</q1:EntityName>" +
            "<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
            "<q1:Attributes>" +
            "<q1:Attribute>" + resultAttribute + "</q1:Attribute>" +
            "</q1:Attributes>" +
            "</q1:ColumnSet>" +
            "<q1:Distinct>false</q1:Distinct>" +
            "<q1:Criteria>" +
            "<q1:FilterOperator>And</q1:FilterOperator>" +
            "<q1:Conditions>" +
            "<q1:Condition>" +
            "<q1:AttributeName>" + queryAttribute + "</q1:AttributeName>" +
            "<q1:Operator>Like</q1:Operator>" +
            "<q1:Values>" +
            "<q1:Value xsi:type='xsd:string'>" + queryValue + "</q1:Value>" +
            "</q1:Values>" +
            "</q1:Condition>" +
            "</q1:Conditions>" +
            "</q1:Criteria>" +
            "</query>" +
            "</RetrieveMultiple>" +
            "</soap:Body>" +
            "</soap:Envelope>";
        var oXMLHTTPReq = new ActiveXObject("Msxml2.XMLHTTP");
        oXMLHTTPReq.onreadystatechange = function() {
            if (oXMLHTTPReq.readyState == 4) {
                WSResultHandler(oXMLHTTPReq.responseXML, resultAttribute, queryAttribute);
            }
        };
        oXMLHTTPReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        oXMLHTTPReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        oXMLHTTPReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        oXMLHTTPReq.setRequestHeader("Content-Length", oXMLSoapMsg.length);
        oXMLHTTPReq.send(oXMLSoapMsg);
    }
    catch (ex) { }
}
function WSResultHandler(resultSetXML, resultAttribute, queryAttribute) {
    try {
        var errorCount = resultSetXML.selectNodes('//error').length;
        if (errorCount != 0) {
            var msg = resultSetXML.selectSingleNode('//description').nodeTypedValue;
            alert(msg);
        }
        else {
            var results = resultSetXML.getElementsByTagName('BusinessEntity');
            var resultsOverview = "";
            if (results.length != 0) {
                resultsOverview += "<div style=\"z-index:+300; padding:5px; font-size:xx-small; border: solid 1px #C5C5C5; background-color:#FFFFAE; position:absolute; filter:alpha(opacity:90);\">";
                resultsOverview += "<strong>E-Mail Anlagen</strong>";
                resultsOverview += "<table border=\"0\"  style=\"font-size: xx-small\">";
                var cntFile = 0;
                var lenTitle = 0;
                for (i = 0; i < results.length; i++) {
                    resultsOverview += "<tr><td><img src=\"/_imgs/ico_16_1001_d.gif\" alt=\"Dateianlage\" /></td><td width=\"100%\" style=\"color:" + oNotesDetailColorFile + ";\">" + results[i].selectSingleNode('./q1:' + resultAttribute).nodeTypedValue + "</td></tr>";
                    lenTitle = (lenTitle < results[i].selectSingleNode('./q1:' + resultAttribute).nodeTypedValue.toString().length) ? results[i].selectSingleNode('./q1:' + resultAttribute).nodeTypedValue.toString().length : lenTitle;
                    cntFile++;
                }
                resultsOverview += "</table>";
                resultsOverview += "</div>";
                oNotesTab.firstChild.style.color = '#EE6000';
                oNotesTab.firstChild.innerHTML += ' (<strong>' + results.length.toString() + '</strong>)';
                oNotesTab.parentNode.style.width = (parseInt(oNotesTab.parentNode.style.width.substring(0, oNotesTab.parentNode.style.width.indexOf("px"))) + 45).toString() + 'px';
                if (oNotesOverview) {
                    var divNotesCountOV = document.createElement("div");
                    divNotesCountOV.setAttribute('id', 'NotesCountOV');
                    divNotesCountOV.style.visibility = "hidden";
                    divNotesCountOV.style.width = (lenTitle <= 70) ? parseInt((lenTitle * 6)).toString() + "px" : "420px";
                    document.body.appendChild(divNotesCountOV);
                    divNotesCountOV.innerHTML = divNotesCountOV.innerHTML + resultsOverview;
                    oNotesTab.firstChild.attachEvent('onmouseenter', displayNotesOverview);
                    oNotesTab.firstChild.attachEvent('onmouseleave', displayNotesOverview);
                }
            }
        }
    }
    catch (e) { }
}
if (crmForm.ObjectId != null) {
    WSRetrieveMultiple('activitymimeattachment', 'filename', 'activityid', crmForm.ObjectId.toString());
}
/* END *************************************************** */
/* NotesCount                                              */
/* ******************************************************* */